<?php

namespace App\Console\Commands;

use App\Models\CompanyOilCard;
use App\Models\OilCard;
use App\Models\RechargeOrder;
use App\Models\ShopOrder;
use App\Models\UserCoupon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class HandleRechargeOrder extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'recharge:close';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        try {
            $tasks = RechargeOrder::where("status", 0)->orderBy("created_at", "desc")->get();
            foreach ($tasks as $k => $v) {
                $shopOrder = ShopOrder::where("id", $v->shop_order_id)->first();
                if ($shopOrder) {
                    //查询油卡
                    $oilCardData = OilCard::where("id", $v->oil_card_id)->first();
                    $companyOilCardData = CompanyOilCard::where("card_no", $oilCardData->card_no)->first();
                    //计算时间
                    $orderTime = strtotime($shopOrder->created_at) + 60 * 60 * 24;
                    if ($orderTime < time()) {
                        //取消订单
                        DB::beginTransaction();
                        $v->status = -1;
                        $v->save();
                        if ($v->coupon_id){
                            $userCoupon = UserCoupon::find($v->coupon_id);
                            if ($userCoupon->status==4){
                                $userCoupon->status=1;
                                $userCoupon->save();
                            }
                        }

                        $shopOrder->order_status = -1;
                        $shopOrder->save();
                        if ($companyOilCardData) {
                            $companyOilCardData->status = 0;
                            $companyOilCardData->used_at = (NULL);
                            $companyOilCardData->user_id = 0;
                            $companyOilCardData->save();
                        }
                        DB::commit();
                    }
                } else {
                    //计算时间
                    $orderTime = strtotime($v->created_at) + 60 * 60 * 24 * 7;
                    if ($orderTime < time()){
                        $v->status = -1;
                        $v->save();
                        if ($v->coupon_id){
                            $userCoupon = UserCoupon::find($v->coupon_id);
                            if ($userCoupon->status==4){
                                $userCoupon->status=1;
                                $userCoupon->save();
                            }
                        }
                    }
                }
            }

        }catch (\Exception $e){
            DB::rollBack();
            Log::error($e);
        }
        $this->line('done');
    }
}
